home *** CD-ROM | disk | FTP | other *** search
/ PC Users 1999 May / Cd Pc Users extra 20 mayo 1999.iso / Prog / Inst / FTP / GETALL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-18  |  3.7 KB  |  145 lines

  1. /*
  2. **  GETALL.C
  3. **
  4. **  This program downloads all files with a specified file extension from 
  5. **  a FTP server. To use this program, first edit the #define's below
  6. **  (SRVR_NAME, USER_NAME, etc.). Note that file extensions are case sensitive.
  7. **  Be sure to set IS_BINARY to TRUE if binary files are to be downloaded.
  8. **
  9. */
  10.  
  11. #include <windows.h>
  12. #include <winsock.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include "fce.h"
  18.  
  19. #define SRVR_NAME   "10.0.0.1"
  20. #define USER_NAME   "mike"
  21. #define PASSWORD    "mike"
  22. #define DIRECTORY   "."
  23. #define FILE_EXT    ".TXT"
  24. #define IS_BINARY   FALSE
  25.  
  26. #define LIST_SIZE 5000
  27. #define LINE_SIZE  100
  28.  
  29. char LineBuffer[LINE_SIZE];
  30. char ListBuffer[LIST_SIZE];
  31.  
  32. char Temp[51];
  33.  
  34. /* globals */
  35.  
  36. void ShowError(int Code)
  37. {Temp[0] = '\0';
  38.  fceErrorText(0,Code,(LPSTR)Temp,50);
  39.  printf("ERROR %d: %s\n", Code, Temp);
  40. }
  41.  
  42. void ErrorExit(int Code)
  43. {ShowError(Code);
  44.  fceRelease();
  45.  exit(1);
  46. }
  47.  
  48. LPSTR FindExtension(LPSTR Filename)
  49. {int i, Len;
  50.  char c;
  51.  LPSTR Ptr;
  52.  Len = strlen(Filename);
  53.  if(Len<=1) return NULL; 
  54.  Ptr = Filename + Len - 1;
  55.  for(i=Len-1;i>0;i--)
  56.    {c = *Ptr;
  57.     if(c=='.') return Ptr;
  58.     Ptr--;
  59.    }
  60.  return NULL;
  61. }
  62.  
  63. void main(int argc, char *argv[])
  64. {int n, Code;
  65.  int FileCount = 0;
  66.  int Version;
  67.  int Build;
  68.  LPSTR Ptr;
  69.  /* check arguments */
  70.  if(argc!=1)
  71.    {printf("Usage: GETALL\n");
  72.     exit(1);
  73.    }
  74.  printf("GETALL 03/01/1999\n");
  75.  printf("Server : %s\n", SRVR_NAME);
  76.  printf("  User : %s\n", USER_NAME);
  77.  printf("  Pass : %s\n", PASSWORD);
  78.  printf("   Dir : %s\n", DIRECTORY);
  79.  printf(" Files : *%s\n", FILE_EXT);
  80.  /* attach FCE */
  81.  Code = fceAttach(1);
  82.  if(Code<0) ErrorExit(Code);
  83.  Version = fceGetInteger(0,FCE_GET_VERSION);
  84.  Build   = fceGetInteger(0,FCE_GET_BUILD);
  85.  printf("FCE32 Version: %1d.%1d.%1d Build %d\n",
  86.     0x0f&(Version>>8),0x0f&(Version>>4),0x0f&Version,Build);
  87.  fceGetString(0,FCE_GET_REGISTRATION,(LPSTR)Temp,50);
  88.  printf(" Registration: %s\n", Temp);
  89.  /* define LOG file */
  90.  ///Code = fceSetString(0,FCE_SET_LOG_FILE,(LPSTR)"getall.log");
  91.  /* connect to server */
  92.  printf("Connecting to %s ... ",SRVR_NAME);
  93.  Code = fceConnect(0,(LPSTR)SRVR_NAME,(LPSTR)USER_NAME,(LPSTR)PASSWORD);
  94.  if(Code<0) ErrorExit(Code);
  95.  printf("OK\n");
  96.  /* change to proper directory ["." is current directory] */
  97.  if(strcmp(DIRECTORY,".")!=0)
  98.    {printf("Changing to server directory '%s'\n", DIRECTORY);
  99.     Code = fceSetServerDir(0, (LPSTR)DIRECTORY);
  100.     if(Code<0) ErrorExit(Code);
  101.    }
  102.  /* set transfer mode */
  103. #if IS_BINARY 
  104.    {printf("Setting BINARY transfer mode.\n");
  105.     fceSetMode(0,'B');
  106.    }
  107. #else
  108.    {printf("Setting ASCII transfer mode.\n"); 
  109.     fceSetMode(0,'A');
  110.    }
  111. #endif   
  112.  /* get file list */
  113.  Code = fceGetList(0, FCE_NAME_LIST, (LPSTR)ListBuffer, LIST_SIZE);
  114.  if(Code<0) ErrorExit(Code);
  115.  /* get each filename */
  116.  for(n=1;;n++)
  117.    {/* extract next filename from downloaded list */
  118.     Code = fceExtract((LPSTR)ListBuffer, n, 0, (LPSTR)LineBuffer, LINE_SIZE);
  119.     if(Code<=1) break;
  120.     ///printf("Filename = [%s]\n", LineBuffer);
  121.     /* find filename extention */
  122.     Ptr = FindExtension(LineBuffer);
  123.     if(Ptr) 
  124.       {/* does extension match ? */    
  125.        if(strcmp(Ptr,(LPSTR)FILE_EXT)==0)
  126.          {/* download this file */
  127.           printf("Downloading file '%s' ... ",LineBuffer);
  128.           Code = fceGetFile(0,(LPSTR)LineBuffer); 
  129.           printf("OK\n");
  130.           if(Code<0) ErrorExit(Code);
  131.           FileCount++;
  132.          }
  133.        else
  134.          {/* skip this file */
  135.           printf("Skipping file '%s'\n",LineBuffer);
  136.          }
  137.       }
  138.    } 
  139.  printf("%d files downloaded.\n", FileCount);  
  140.  /* QUIT */
  141.  fceClose(0);
  142.  fceRelease();
  143. }
  144.  
  145.